/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode.interview.linklist;

import com.xiaohujr.xuexue.leetcode.tree.ListNode;

/**
 *
 *     * https://leetcode-cn.com/problems/reverse-linked-list/
 *
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2021/1/8
 */
public class LinkedListReverse {
    public ListNode reverseList(ListNode head) {
        ListNode curNode = head;
        ListNode preNode = null;
        while (curNode != null) {
            ListNode nextNode = curNode.next;
            curNode.next = preNode;
            preNode = curNode;
            curNode = nextNode;
        }
        return preNode;
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        ListNode d = new ListNode(4);
        a.next = b;
        b.next = c;
        c.next = d;
        new LinkedListReverse().reverseList(a);
    }
}
